package com.aliyun.messagemgt.util;

import com.aliyun.messagemgt.common.enums.TimeTypeEnum;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

@SuppressWarnings("ALL")
public class DateUtil {
	private static Logger log = Logger.getLogger(DateUtil.class.getName());

	// 系统默认日期格式
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	// 系统默认日期时间格式
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	// 8位日期格式
	public static final String DATE_FORMAT_8 = "yyyyMMdd";
	// 12位日期格式
    public static final String DATE_FORMAT_12 = "yyyyMMddHHmm";
	// 14位日期时间格式
	public static final String DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
	
	// 时分
    public static final String HOUR_TIME_FORMAT_9 = "HH:mm";
    
    // 时分秒
    public static final String HOUR_TIME_FORMAT_10 = "HH:mm:ss";

	/**
	 * 将Date转换成yyyy-MM-dd HH:mm:ss
	 * @return
	 */
    public static String getStringDateFromDate(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
    	return sdf.format(date);
    }
	public static Date getCurrentTime() {
		return new Date();
	}
	
	public static Timestamp getTimeStamp(Date date) {
		if (date == null) {
			return null;
		}
		else {
			return new Timestamp(date.getTime());
		}
	}
	public static Timestamp getTimeStamp(String strdate) {
		if (strdate == null) {
			return null;
		}
		else {
			Date date = getFormated8Date(strdate);
			if(null != date){
				return new Timestamp(date.getTime());
			}
		}
		return null;
	}
	public static java.sql.Date getDate(Date date) {
		if (date == null) {
			return null;
		}
		else {
			return new java.sql.Date(date.getTime());
		}
	}

	/**
	 * 将Date转换成统一的日期格式文本。
	 * 
	 * @return
	 */
	public static String getFormatedDate(Date date) {
		if (null == date) {
			return "";
		}

		SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT);
		return dateFormator.format(new java.sql.Date(date.getTime()));
	}

	public static Date getFormatedDate(String text) {
		if (null == text) {
			return null;
		}

		try {
			SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT);
			return dateFormator.parse(text);
		} catch (Exception e) {
			log.log(Level.SEVERE, "Error while switch data format " + text, e);
		}

		return null;
	}
	public static Date getFormated8Date(String text) {
		if (null == text) {
			return null;
		}

		try {
			SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT_8);
			return dateFormator.parse(text);
		} catch (Exception e) {
			log.log(Level.SEVERE, "Error while switch data format " + text, e);
		}

		return null;
	}
	/**
	 * 将Date转换成统一的日期时间格式文本。
	 * 
	 * @return
	 */
	public static String getFormatedDateTime(Date date) {
		if (null == date) {
			return "";
		}

		SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_TIME_FORMAT);
		return dateFormator.format(new java.sql.Date(date.getTime()));
	}

	public static Date getFormatedDateTime(String text) {
		if (null == text) {
			return null;
		}

		try {
			SimpleDateFormat dateFormator = new SimpleDateFormat(
					DATE_TIME_FORMAT);
			return dateFormator.parse(text);
		} catch (Exception e) {
			log.log(Level.SEVERE, "Error while switch datatime format " + text,
					e);
		}

		return null;
	}
	
	/**
     * 将long型时间值转换为日期格式。
     *
     * @return
     */
    public static String getFormatedDateTime(long time) {
        if (0 == time) {
            return "0";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_TIME_FORMAT);
        return dateFormator.format(new java.sql.Date(time));
    }

	/**
	 * 将Date转换成统一的日期时间格式文本。
	 * 
	 * @return
	 */
	public static String getFormatedDateTime(Timestamp stamp) {
		if (null == stamp) {
			return "";
		}

		SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_TIME_FORMAT);
		return dateFormator.format(new java.sql.Date(stamp.getTime()));
	}

	/**
	 * 将Date转换成统一的日期格式文本。 格式：yyyy-mm-dd
	 * 
	 * @return
	 * @author suns
	 */
	public static String getFormatedDate(Timestamp stamp) {
		if (null == stamp) {
			return "";
		}

		SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT);
		return dateFormator.format(new java.sql.Date(stamp.getTime()));
	}
	
   public static String getFormatedDateTime12(Date date) {
        if (null == date) {
            return "";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT_12);
        return dateFormator.format(new java.sql.Date(date.getTime()));
    }
	
	/**
     * 将long型时间值转换为日期格式。
     *
     * @return
     */
    public static String getFormatedDateTime14(long time) {
        if (0 == time) {
            return "0";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_TIME_FORMAT_14);
        return dateFormator.format(new java.sql.Date(time));
    }
	
	public static String getFormatedDateTime14(Date date) {
		if (null == date) {
			return "";
		}

		SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_TIME_FORMAT_14);
		return dateFormator.format(new java.sql.Date(date.getTime()));
	}
	
	public static Date getFormatedDateTime14(String text) {
		if (null == text) {
			return null;
		}

		try {
			SimpleDateFormat dateFormator = new SimpleDateFormat(
					DATE_TIME_FORMAT_14);
			return dateFormator.parse(text);
		} catch (Exception e) {
			log.log(Level.SEVERE, "Error while switch datatime format " + text,
					e);
		}

		return null;
	}
	
	 /**
     * 将long型时间值转换为日期格式。hh:mm
     *
     * @return
     */
    public static String getHourTimeFormat9Time(long time) {
        if (0 == time) {
            return "0";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(HOUR_TIME_FORMAT_9);
        return dateFormator.format(new java.sql.Date(time));
    }
    
    /**
     * 将long型时间值转换为日期格式。yyyyMMdd
     *
     * @return
     */
    public static String getFormat8Time(long time) {
        if (0 == time) {
            return "0";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(DATE_FORMAT_8);
        return dateFormator.format(new java.sql.Date(time));
    }
    
    /**
     * 将long型时间值转换为日期格式。hh:mm:ss
     *
     * @return
     */
    public static String getHourTimeFormat10Time(long time) {
        if (0 == time) {
            return "0";
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat(HOUR_TIME_FORMAT_10);
        return dateFormator.format(new java.sql.Date(time));
    }
	
	/**
     * 将Date转换成统一的时间格式文本 hh:mm。
     * 
     * @return
     */
    public static String getHourTimeFormat9Time(Timestamp stamp) {
        if (null == stamp) {
            return "";
        }

        SimpleDateFormat dateFormator = new SimpleDateFormat(HOUR_TIME_FORMAT_9);
        return dateFormator.format(new java.sql.Date(stamp.getTime()));
    }

	/**
	 * 得到应用服务器当前日期，按照指定的格式返回。
	 *
	 * @param pattern
	 *            格式类型，通过系统常量中定义，如：CapConstants.DATE_FORMAT_8
	 * @return
	 */
	public static String formatNowDate(String pattern) {
		Date date = new Date();
		SimpleDateFormat dateFormator = new SimpleDateFormat(pattern);
		return dateFormator.format(date);
	}

	 /**
     * 根据时间类型比较时间大小 
     * 
     * @param source
     * @param traget
     * @param  "YYYY-MM-DD" "yyyyMMdd HH:mm:ss"  类型可自定义
     * @param
     * @return 
     *  0 ：source和traget时间相同    
     *  1 ：source比traget时间大  
     *  -1：source比traget时间小
     * @throws Exception
     */
    public static int dateCompare(String source, String traget) throws Exception {
        int ret;
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        Date sourcedate = format.parse(source);
        Date tragetdate = format.parse(traget);
        ret = sourcedate.compareTo(tragetdate);
        return ret;
    }

	public static boolean after(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
    	c1.setTime(date1);
    	Calendar c2 = Calendar.getInstance();
    	c2.setTime(date2);
    	return c2.after(c1);
	}

	public static String formatDate(Date date, String pattern) {
		if (date == null){
			return null;
		}
		if (pattern == null) {
			throw new IllegalArgumentException("pattern is null");
		} else {
			SimpleDateFormat formatter = new SimpleDateFormat(pattern);
			return formatter.format(date);
		}
	}

	public static Date getCurrnetDate() {
		return new Date();
	}

	public static int getTimes(Date currnetDate,int flag){
    	if(currnetDate == null){
    		return -1;
		}
    	//获取枚举对象
		TimeTypeEnum timeTypeEnum = TimeTypeEnum.valueofKey(flag);
		Calendar ca = Calendar.getInstance();
		ca.setTime(currnetDate);
		switch (timeTypeEnum){
			case YEAR:
				//获取年份
				return ca.get(Calendar.YEAR);
			case MONTH:
				//获取月份
				return ca.get(Calendar.MONTH)+1;
			case DAY:
				//获取日
				return ca.get(Calendar.DATE);
			case HOUR:
				//小时Calendar.HOUR 12小时制
				return ca.get(Calendar.HOUR_OF_DAY);
			case MINUTE:
				//分
				return ca.get(Calendar.MINUTE);
			case SECOND:
				//秒
				return ca.get(Calendar.SECOND);
			case WEEKOFYEAR:
				//显示今天是一周的第几天
				//以星期一为本周的第一天(不减1 是以周日为本周开始第一天)
				 int dayOfWeek =  ca.get(Calendar.DAY_OF_WEEK)- 1;
				if (dayOfWeek == 0 ) {
					dayOfWeek = 7 ;
				}
				return dayOfWeek;
			default:
				return -1;
		}
	}

	public static void main(String[] args) {
    	/*Date date = DateUtil.getFormatedDateTime("2020-12-26 00:01:03");
		System.out.println("num date = [" + date.toString() + "]");
		int num = getTimes(date,TimeTypeEnum.MONTH.getValue());
		System.out.println("num MONTH = [" + num + "]");
		 num = getTimes(date,TimeTypeEnum.YEAR.getValue());
		System.out.println("num YEAR = [" + num + "]");
		 num = getTimes(date,TimeTypeEnum.DAY.getValue());
		System.out.println("num  DAY= [" + num + "]" +"\t"+date.getDate());
		 num = getTimes(date,TimeTypeEnum.HOUR.getValue());
		System.out.println("num  HOUR= [" + num + "]"+"\t"+date.getHours());
		 num = getTimes(date,TimeTypeEnum.MINUTE.getValue());
		System.out.println("num MINUTE= [" + num + "]"+"\t"+date.getMinutes());
		 num = getTimes(date,TimeTypeEnum.SECOND.getValue());
		System.out.println("num SECOND= [" + num + "]");
		 num = getTimes(date,TimeTypeEnum.WEEKOFYEAR.getValue());
		System.out.println("num WEEKOFYEAR= [" + num + "]");*/
	}
    
}
